package copyengine.sceneAndDialog.scene.impl
{
    import copyengine.sceneAndDialog.CETreeStructureComponent;
    import copyengine.sceneAndDialog.scene.ICEScene;
    import copyengine.utils.GeneralUtils;
    import copyengine.utils.ICEDispose;

    import starling.display.DisplayObjectContainer;
    import starling.display.Sprite;

    public class CESceneBasic extends CETreeStructureComponent implements ICEScene
    {
        private var mEmptyContainer:Sprite;
        private var mAllDisposableObjectVector:Vector.<ICEDispose>;
        private var mIsAlreadyDispose:Boolean;

        public function CESceneBasic()
        {
        }

        public function onAddToSceneManger():void
        {
        }

        public function get sceneContainer():DisplayObjectContainer
        {
            if (isUseEmptySceneContainer()) {
                if (mEmptyContainer == null) {
                    mEmptyContainer = new Sprite();
                }
                return mEmptyContainer;
            } else {
                throw new Error("Child class should override this function or override isUseEmptySceneContainer and return true");
            }
        }

        protected function isUseEmptySceneContainer():Boolean {return false}


        override public function dispose():void
        {
            if (!mIsAlreadyDispose) {
                mIsAlreadyDispose = true;
                if (mAllDisposableObjectVector != null) {
                    for each(var obj:ICEDispose in mAllDisposableObjectVector) {
                        obj.dispose();
                    }
                    mAllDisposableObjectVector = null;
                }
                super.dispose();
            }

        }

        public final function addDisposableObject(_obj:ICEDispose):void
        {
            if (mIsAlreadyDispose) {return;}
            if (mAllDisposableObjectVector == null) {
                mAllDisposableObjectVector = new Vector.<ICEDispose>();
            }
            GeneralUtils.addElementToArrayOrVector(_obj, mAllDisposableObjectVector);
        }

        public final function removeDisposableObject(_obj:ICEDispose):void
        {
            //防止Scene Dispose函数 调用obj自己的dispose 然后该dispose又调用了removeDisposableObject
            if (mIsAlreadyDispose) {return;}
            GeneralUtils.removeElementFromArrayOrVector(_obj, mAllDisposableObjectVector);
        }

    }
}
